package com.drug.web.controller.wms;

import com.drug.common.annotation.Log;
import com.drug.common.core.controller.BaseController;
import com.drug.common.core.domain.AjaxResult;
import com.drug.common.core.page.TableDataInfo;
import com.drug.common.enums.BusinessType;
import com.drug.common.utils.poi.ExcelUtil;
import com.drug.wms.domain.BusSkuStock;
import com.drug.wms.service.IBusSkuStockService;
import com.drug.wms.vo.BusSkuStockVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 库存Controller
 *
 * @author ruoyi
 * @date 2021-12-05
 */
@RestController
@RequestMapping("/system/stock")
@Api(description = "库存")
public class BusSkuStockController extends BaseController {
    @Autowired
    private IBusSkuStockService busSkuStockService;

    /**
     * 查询库存列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:list')")
    @ApiOperation("查询库存列表")
    @GetMapping("/list")
    public TableDataInfo list(BusSkuStock busSkuStock) {
        startPage();
        List<BusSkuStock> list = busSkuStockService.selectBusSkuStockList(busSkuStock);
        return getDataTable(list);
    }

    /**
     * 导出库存列表
     */
    @PreAuthorize("@ss.hasPermi('system:stock:export')")
    @Log(title = "库存", businessType = BusinessType.EXPORT)
    @ApiOperation("导出库存列表")
    @GetMapping("/export")
    public AjaxResult export(BusSkuStock busSkuStock) {
        List<BusSkuStock> list = busSkuStockService.selectBusSkuStockList(busSkuStock);
        ExcelUtil<BusSkuStock> util = new ExcelUtil<BusSkuStock>(BusSkuStock.class);
        return util.exportExcel(list, "库存数据");
    }

    /**
     * 获取库存详细信息
     */
    @ApiOperation("获取库存详细信息")
    @PreAuthorize("@ss.hasPermi('system:stock:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(busSkuStockService.selectBusSkuStockDetailsById(id));
    }

    /**
     * 获取库存详细信息
     */
    @ApiOperation("根据商品编号获取库存详细信息")
    @PreAuthorize("@ss.hasPermi('system:stock:query.skuid')")
    @GetMapping(value = "/sku")
    public AjaxResult getInfoBySkuId(@RequestParam("skuId") String skuId, @RequestParam(value = "deptId", required = false) Long deptId) {
        return AjaxResult.success(busSkuStockService.selectBusSkuStockBySkuId(skuId, deptId));
    }


    /**
     * 新增库存
     */
    @PreAuthorize("@ss.hasPermi('system:stock:add')")
    @Log(title = "库存", businessType = BusinessType.INSERT)
    @ApiOperation("新增库存")
    @PostMapping
    public AjaxResult add(@RequestBody List<BusSkuStock> busSkuStockList) {
        return toAjax(busSkuStockService.insertOrUpdateSkuStock(busSkuStockList));
    }

    /**
     * 修改库存
     */
    @PreAuthorize("@ss.hasPermi('system:stock:edit')")
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @ApiOperation("修改库存")
    @PutMapping
    public AjaxResult edit(@RequestBody BusSkuStock busSkuStock) {
        return toAjax(busSkuStockService.updateBusSkuStock(busSkuStock));
    }

    /**
     * 变更基本单位（零整库移库操作）
     */
    @Log(title = "库存", businessType = BusinessType.UPDATE)
    @ApiOperation("变更基本单位（零整库移库操作）")
    @PostMapping(value = "/changeBaseUnit")
    public AjaxResult changeBaseUnit(@RequestBody BusSkuStockVo busSkuStockVo) {
        return toAjax(busSkuStockService.changeBaseUnit(busSkuStockVo.getSkuStockId(),
                busSkuStockVo.getWareHoseId(), busSkuStockVo.getMoveNum()));
    }
}
